package com.cn.medicalcare.controller.backstageservlet.company;

import com.cn.medicalcare.customtool.JsonTool;
import com.cn.medicalcare.customtool.SqlSessionFactoryTool;
import com.cn.medicalcare.mapper.CompanyIntroduceMapper;
import com.cn.medicalcare.pojo.CompanyIntroduce;
import com.cn.medicalcare.service.CompanyIntroduceService;
import com.cn.medicalcare.service.serviceimpl.CompanyIntroduceServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map;

@WebServlet("/backstage/company/updatecompanyintroduce.json")
public class CompanyIntroduceUpdateServlet extends HttpServlet {

    private CompanyIntroduceService companyIntroduceService = CompanyIntroduceServiceImpl.getInstance();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String title = req.getParameter("title");
        String img = req.getParameter("img");
        String content = req.getParameter("content");
        String survival = req.getParameter("survival");

        PrintWriter pw = resp.getWriter();
        Map<String, Object> map = new LinkedHashMap<>();
        CompanyIntroduce companyIntroduce = new CompanyIntroduce();
        try {
            companyIntroduce.setId(Integer.valueOf(id));
            companyIntroduce.setSurvival(Integer.valueOf(survival));
            companyIntroduce.setTitle(title).setImg(img).setContent(content);
            if (companyIntroduceService.updateCompanyIntroduce(companyIntroduce)) {
                map.put("status", 200);
            } else {
                map.put("status", 500);
            }
        } catch (Exception e) {
            map.put("status", 400);
        }
        pw.print(JsonTool.toJson(map));
    }
}
